package com.hqs.rxjava;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hqs.common.utils.ScreenUtils;

import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> items;
    private RecyclerView recyclerView;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayout = new LinearLayoutManager(this);
        linearLayout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayout);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

        final float density = ScreenUtils.density(this);

        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

            private Paint paint;
            private int dividerH = (int) (density * 20);
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

                if (paint == null){
                    paint = new Paint();
                    paint.setColor(Color.LTGRAY);
                    paint.setStyle(Paint.Style.FILL);
                }

                int left = parent.getPaddingLeft();
                int right = parent.getWidth() - parent.getPaddingRight();
                final int childCount = parent.getChildCount();
                for (int i = 0; i < childCount; i++){
                    final View child = parent.getChildAt(i);

                    //获得child的布局信息
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
                    final int top = child.getBottom() + params.bottomMargin;
                    final int bottom = top + dividerH;
                    c.drawRect(left, top, right,bottom, paint);
                }
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                // 设置绘制divider的rect
                outRect.set(0, 0, 0, dividerH);
            }
        });

        Observable.create(new ObservableOnSubscribe<ArrayList<String>>() {
                    @Override
                    public void subscribe(@NonNull ObservableEmitter<ArrayList<String>> e) throws Exception {
                        // 模拟网络延迟
                        Thread.sleep(1000);
                        items = new ArrayList<>();

                        // 0
                        items.add("模拟用户登录");
                        // 1
                        items.add("使用buffer模拟双击");
                        // 2
                        items.add("Retrofit 网络请求");
                        // 3
                        items.add("同时监听两个View");
                        // 4
                        items.add("邮箱校验");
                        // 5
                        items.add("retry");
                        // 6
                        items.add("按钮事件");

                        for (int i = 0; i < 100; i++){
                            items.add("item_" + i);
                        }
                        e.onNext(items);
                        e.onComplete();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ArrayList<String>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull ArrayList<String> array) {
                        adapter.setItems(array);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                    }
        });

    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        private ArrayList<String> items;

        public void setItems(ArrayList<String> items) {
            this.items = items;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_view_holder, null);
            ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(param);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            holder.tv.setText(items.get(position));

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickAt(position);
                }
            });

        }

        @Override
        public int getItemCount() {
            if (items == null){
                return 0;
            }
            return items.size();
        }
    }

    private void onClickAt(int position){
        switch (position){
            case 0:
                startActivity(LoginActivity.class);
                break;
            case 1:
                startActivity(DoubleClickActivity.class);
                break;
            case 2:
                startActivity(RetrofitActivity.class);
                break;
            case 3:
                startActivity(SumActivity.class);
                break;
            case 4:
                startActivity(ValidateActivity.class);
                break;
            case 5:
                startActivity(RetryActivity.class);
                break;
            case 6:
                startActivity(ButtonClickActivity.class);
                break;

            default:

        }
    }

    private void startActivity(Class cls){
        Intent intent = new Intent(MainActivity.this, cls);
        startActivity(intent);
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_title);
        }
    }
}
